package pkg.action;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.stripes.action.After;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.ValidationErrors;
import pkg.dao.EntityDAO;
import pkg.dao.PagedList;
import pkg.entity.Teacher;
import pkg.entity.Auction;
import pkg.filter.TeacherFilter;
import pkg.filter.AuctionFilter;
import pkg.util.ImageUtil;

public class AuctionActionBean extends BaseActionBean {

    private PagedList<Auction> auctions;
    private List<Auction> auctionList;
    private List<Teacher> teachers;
    private List<Long> auctionIds;
    private Integer page;
    private String sort;
    private String dir;
    private String mode;
    private String filterText;
    private Auction auction;
    private FileBean picture;

    public AuctionActionBean() {
    }

    @DefaultHandler
    public Resolution view() {
        loadAuctions();
        return new ForwardResolution("/AuctionList.jsp");
    }

    public void loadAuctions() {
        EntityDAO sb;

        sb = new EntityDAO();
        //TODO
        AuctionFilter f = new AuctionFilter();
        filterText = (String) getSession().getAttribute("filterText");
        if (filterText != null) {
            f.setSimple(filterText);
        }
        auctionList = sb.findAllByFilter(f);
        auctions = new PagedList<Auction>();
        auctions.setResults(sb.findAllByFilter(f));
        if (page == null) {
            page = 1;
        }
        auctions.setPageNumber(page);
    }

    public Resolution addNew() {
        return new RedirectResolution("/Auction.action?create");
    }

    public Resolution deleteSelected() {
        EntityDAO sb = new EntityDAO();

        if (getAuctionIds() != null) {
            sb.delete(Auction.class, auctionIds);
        }
        loadAuctions();
        RedirectResolution result;
        result = new RedirectResolution(this.getClass());
        result.addParameter("page", page);
        return result;
    }

    public Resolution preEdit() {
        EntityDAO sb;
        sb = new EntityDAO();
        auction = sb.find(Auction.class, auction.getId());
        loadLookups();
        return new ForwardResolution("/AuctionEdit.jsp");
    }

    public Resolution save() throws IOException {
        EntityDAO sb = new EntityDAO();
        Auction origItem = null;

        if (auction.getId() != null) {
            origItem = sb.find(Auction.class, auction.getId());
        }

        // Link up parent to children
        if (picture != null) {
            byte[] newImage = ImageUtil.resizeImageStream(picture.getInputStream(), 100, 150);
            if (newImage == null) {
                addError(picture.getFileName() + " is not a valid image.");
                return getContext().getSourcePageResolution();
            }
            auction.setPicBlob(newImage);
        }

        if(auction.getAmount() == null) {
            auction.setAmount(auction.getStartAmount());
        }
        sb.update(auction);
        RedirectResolution result;
        result = new RedirectResolution(this.getClass());
        result.addParameter("page", page);
        return result;
    }

    public Resolution image() {
        EntityDAO dao = new EntityDAO();

        auction = dao.find(Auction.class, auction.getId());

        Image image = ImagesServiceFactory.makeImage(auction.getPicBlob());
        return new StreamingResolution(ImageUtil.getContentType(image), new ByteArrayInputStream(image.getImageData()));
    }

    @After(stages = LifecycleStage.BindingAndValidation)
    public void loadRequestIfErrors() {
        ValidationErrors ve = getContext().getValidationErrors();
        if (ve.size() > 0 && auction != null && auction.getId() != null) {
            EntityDAO sb = new EntityDAO();
            auction = sb.find(Auction.class, auction.getId());
        }
        loadLookups();
    }

    public Resolution create() {
        auction = new Auction();
        mode = "add";
        loadLookups();
        return new ForwardResolution("/AuctionEdit.jsp");
    }

    public Resolution applyFilter() {
        if (filterText != null) {
            getSession().setAttribute("filterText", filterText);
        } else {
            getSession().removeAttribute("filterText");
        }
        return new RedirectResolution(this.getClass());
    }

    public void loadLookups() {
        teachers = new ArrayList<Teacher>();
        EntityDAO dao = new EntityDAO();
        TeacherFilter f = new TeacherFilter();
        teachers.addAll(dao.findAllByFilter(f));
    }

    //<editor-fold defaultstate="collapsed" desc=" Setters/Getters ">
    public Auction getAuction() {
        return auction;
    }

    public void setAuction(Auction category) {
        this.auction = category;
    }

    public Auction getItem() {
        return auction;
    }

    public void setItem(Auction item) {
        this.auction = item;
    }

    public PagedList<Auction> getAuctions() {
        return auctions;
    }

    public void setAuctions(PagedList<Auction> items) {
        this.auctions = items;
    }

    public List<Auction> getAuctionList() {
        return auctionList;
    }

    public void setAuctionList(List<Auction> itemList) {
        this.auctionList = itemList;
    }

    public List<Long> getAuctionIds() {
        return auctionIds;
    }

    public void setAuctionIds(List<Long> AuctionIds) {
        this.auctionIds = AuctionIds;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getFilterText() {
        return filterText;
    }

    public void setFilterText(String filterText) {
        this.filterText = filterText;
    }

    public List<Teacher> getTeachers() {
        return teachers;
    }

    public void setTeachers(List<Teacher> teachers) {
        this.teachers = teachers;
    }

    public FileBean getPicture() {
        return picture;
    }

    public void setPicture(FileBean picture) {
        this.picture = picture;
    }
    //</editor-fold>
}
